class Point {

    constructor(x, y) {
        this.x = x;
        this.y = y;
    }

    toString() {
        return '(' + this.x + ', ' + this.y + ')';
    }

}

var point = new Point(2, 3);

console.log(point.toString()); // (2, 3)

console.log(point.hasOwnProperty('x')); // true
console.log(point.hasOwnProperty('y')); // true
console.log(point.hasOwnProperty('toString')); // false
console.log(point.__proto__.hasOwnProperty('toString')); // true


var p1 = new Point(2,3);
var p2 = new Point(3,2);

// 类的所有实例共享一个原型对象
console.log(p1.__proto__ === p2.__proto__); // true

//通过实例的__proto__属性为Class添加方法
p1.__proto__.printName = function () { return 'Oops' };
p2.printName(); // "Oops"

// 类的继承
class ColorPoint extends Point {
    constructor(x, y, color) {
        super(x, y); // 调用父类的constructor(x, y)
        this.color = color;
    }

    toString() {
        return this.color + ' ' + super.toString(); // 调用父类的toString()
    }
}

// 子类的__proto__属性，表示构造函数的继承，总是指向父类
console.log(ColorPoint.__proto__ === Point); //true

//子类prototype属性的__proto__属性，表示方法的继承，总是指向父类的prototype属性
console.log(ColorPoint.prototype.__proto__ === Point.prototype);//true

